package com.xca.znb.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Switch;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.xca.znb.entity.Local;
import com.xca.znb.model.impl.UpdateLocalImpl;
import com.xca.znb.presenter.UpdateLocalPresenter;

public class UpdatePayLocalActivity extends AppCompatActivity {
    private ImageButton back ;
    private EditText name , phone , local ;
    private Switch state ;
    private Button delete , update ;
    private UpdateLocalPresenter presenter ;
    private String SwitchChange ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_my_local);

        back = findViewById(R.id.update_local_back);
        name = findViewById(R.id.update_local_name);
        phone = findViewById(R.id.update_local_phone);
        local = findViewById(R.id.update_local_text);
        state = findViewById(R.id.update_local_default);
        delete = findViewById(R.id.delete_local_btn);
        update = findViewById(R.id.update_local_btn);

        UpdateLocalImpl updateLocal = new UpdateLocalImpl(this);
        presenter = new UpdateLocalPresenter(updateLocal);

        Intent intent = getIntent() ;
        int getLocalId = intent.getIntExtra("myLocalId", 0);

        Local local1 = presenter.getLocalById(getLocalId);
        name.setText(local1.getName());
        phone.setText(local1.getPhone());
        local.setText(local1.getAddress());
        if (local1.getType().equals("是")){
            state.setChecked(true);
        } else if (local1.getType().equals("否")) {
            state.setChecked(false);
        }


        SwitchChange = local1.getType();
        state.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    SwitchChange = "是";
                }else {
                    SwitchChange = "否";
                }
            }
        });

        update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String uName = name.getText().toString();
                String uPhone = phone.getText().toString();
                String uLocal = local.getText().toString() ;
                String uType = SwitchChange ;

                if (TextUtils.isEmpty(uName) || TextUtils.isEmpty(uPhone) || TextUtils.isEmpty(uLocal)) {
                    Toast.makeText(UpdatePayLocalActivity.this, "所有输入框都必须填写", Toast.LENGTH_SHORT).show();
                }
                if (uPhone.length() != 11){
                    Toast.makeText(UpdatePayLocalActivity.this, "手机号码格式错误", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!uPhone.matches("[1]{1}\\d{10}")){
                    Toast.makeText(UpdatePayLocalActivity.this, "手机号码格式错误", Toast.LENGTH_SHORT).show();
                    return;
                }
                SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
                String login_phone = sharedPreferences.getString("phone" , "");

                if (SwitchChange.equals("是")){
                    presenter.updateDefaultLocal(login_phone);
                }

                presenter.updateLocalById(getLocalId , uName , uPhone , uLocal , uType);
                Local local2 = presenter.getLocalById(getLocalId);
                if (local2.getName().equals(uName)&&local2.getPhone().equals(uPhone)&&local2.getAddress().equals(uLocal)&&local2.getType().equals(uType)){
                    Toast.makeText(UpdatePayLocalActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
//                    Intent intent1 = new Intent(UpdatePayLocalActivity.this , LocalPayActivity.class);
//                    startActivity(intent1);
                    finish();
                }
            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(UpdatePayLocalActivity.this)
                        .setMessage("确定删除该地址吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                presenter.deleteLocalById(getLocalId);
                                Local local2 = presenter.getLocalById(getLocalId);
                                if (local2 == null){
                                    Toast.makeText(UpdatePayLocalActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
//                                    Intent intent1 = new Intent(UpdatePayLocalActivity.this , LocalPayActivity.class);
//                                    startActivity(intent1);
                                    finish();
                                }else {
                                    Toast.makeText(UpdatePayLocalActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setNegativeButton("取消",null)
                        .create()
                        .show();
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent1 = new Intent(UpdatePayLocalActivity.this , LocalPayActivity.class);
//                startActivity(intent1);
                finish();
            }
        });
    }
}
